package com.itheima.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 封装增强代码方法的类
 *  通知类|切面类
 */
@Component//前提：切面类要被IOC容器管理
@Aspect//定义当前类为切面类
public class MyAdvice2 {

    @Pointcut("execution(* com.itheima.service..*.find*(..))")
    public void myPt(){}

    /**
     * 定义环绕通知，内部循环方法1w次，获取运行时长
     * @param pjp
     * @return
     */
    @Around("myPt()")
    public Object takeTime(ProceedingJoinPoint pjp) throws Throwable {
        //获取执行开始时间
        long startTime = System.currentTimeMillis();
        Object result=null;
        for (int i = 0; i < 100000; i++) {
             result = pjp.proceed();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("当前方法运行1w次耗时："+(endTime-startTime)+"ms");
        return result;
    }



}
